package com.md.api.common.utils;

/**
 * @Author liujie
 * @ClassName WordUtils
 * @Date 2022年12月21日13:32
 * @Deacription TODO
 **/

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;

/**
 * Description: Word导出工具类
 *
 * @author yzp
 *
 * @date 2019年9月24日
 *
 */
@Slf4j
public class WordUtils {
    public static File wordCreate(Map<String, Object> dataMap, String fileName, String ftlName) {
        //返回的文件
        File outFile = new File(fileName);
        log.info("outFile:"+outFile);
        //创建配置实例
        Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
        //设置编码
        configuration.setDefaultEncoding("UTF-8");
        //html或ftl模板文件统一放置html或ftl包下
        configuration.setClassForTemplateLoading(WordUtils.class, "/");
        Template template = null;
        try {
            //获取模板
            template = configuration.getTemplate(ftlName);
            //将模板和数据模型合并生成文件
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"UTF-8"));
            //生成文件
            template.process(dataMap, out);
            //关闭流
            out.flush();
            out.close();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return outFile;
    }

    public static void outPutData(HttpServletResponse response, String ftlName, String outFileName, Map<String, Object> dataMap) {
        File file = null;
        InputStream fin = null;
        ServletOutputStream out = null;
        try {
            file = wordCreate(dataMap, outFileName, ftlName);
            fin = new FileInputStream(file);
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/msword");
            response.addHeader("Content-Disposition", "attachment;filename=" + outFileName);
            out = response.getOutputStream();
            byte[] buffer = new byte[1024]; // 缓冲
            int bytesToRead = -1;
            while ((bytesToRead = fin.read(buffer)) != -1) {
                out.write(buffer, 0, bytesToRead);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(fin != null) {
                try {
                    fin.close();
                }catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(file != null) {
                file.delete();
            }
        }
    }
}
